package com.yc.springmvc.web;

import com.yc.springmvc.entity.User;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserDo {

    public UserDo(){
        System.out.println("========UserDo========");
    }

    @RequestMapping("reg")
    public Map<?,?> reg(@Valid User user, Errors errors) {
        if (errors.hasErrors()) {
            List<ObjectError> allErrors = errors.getAllErrors();
            return Map.of("code", 0, "msg", "字段验证错误",
                    "data",allErrors);
        }
        return Map.of("code", 1, "msg", "字段验证通过");
    }

}
